package com.yishusmt.wechatapi.wechatdev.utils;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * @author zhangyi
 * @date 2018/8/11 11:48
 */
@Component
public class HttpClientUtil {
    @Autowired
    private RequestConfig config;
    /**
     * post连接
     *
     * @param url
     * @param jsonBody
     * @return
     * @throws IOException
     */
    public String postConnect(String url, String jsonBody) throws IOException, URISyntaxException {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建HttpPost对象
        HttpPost httpPost = new HttpPost(String.valueOf(new URL(url)));
        //添加基本配置信息
        httpPost.setConfig(config);
        //设置格式
        StringEntity stringEntity = new StringEntity(jsonBody, "utf-8");
        stringEntity.setContentEncoding("UTF-8");
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
        httpPost.setEntity(stringEntity);
        String execute = httpClient.execute(httpPost, new BasicResponseHandler());
        return execute;
    }

    /**
     * 上传文件
     *
     * @param url
     * @param filePath
     * @return
     * @throws IOException
     */
    public String upLoadConnect(String url, String filePath) throws IOException, URISyntaxException {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建HttpPost对象
        HttpPost httpPost = new HttpPost(String.valueOf(new URL(url)));
        //添加基本配置信息
        httpPost.setConfig(config);
        //设置上传格式
        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
        //设置文件路径
        File file = new File(filePath);
        multipartEntityBuilder.addBinaryBody(DateUtil.getLocalTime().toString(),file);
        HttpEntity httpEntity = multipartEntityBuilder.build();
        //设置格式
        httpPost.setEntity(httpEntity);
        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity responseEntity = httpResponse.getEntity();
        int statusCode= httpResponse.getStatusLine().getStatusCode();
        StringBuffer buffer = new StringBuffer();
        if(statusCode == 200){
            BufferedReader reader = new BufferedReader(new InputStreamReader(responseEntity.getContent()));
            String str = "";
            while((str = reader.readLine()) != null) {
                buffer.append(str);
            }
        }
        httpClient.close();
        if(httpResponse!=null){
            httpResponse.close();
        }
        return buffer.toString();
    }
    /**
     * get请求链接
     *
     * @param url
     * @return
     */
    public String getConnect(String url) throws IOException {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(config);
        String execute = httpClient.execute(httpGet, new BasicResponseHandler());
        return execute;
    }
}
